<template>
  <div>
    <el-dialog
      class="my_dialog"
      v-bind="$attrs"
      v-on="$listeners"
      v-draggable
      width="600px"
      :visible="visible"
      @open="onOpen"
      @close="onClose"
      :title="title"
      :close-on-click-modal="false"
    >
      <el-form
        ref="elForm"
        :model="formData"
        :rules="rules"
        size="medium"
        label-width="100px"
      >
        <el-form-item label="编码" prop="code">
          <el-input
            v-model="formData.code"
            placeholder="请输入工作中心编码"
            show-word-limit
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input
            v-model="formData.name"
            placeholder="请输入工作中心名称"
            show-word-limit
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>

        <el-form-item label="部门" prop="departmentID" required>
          <treeselect
            v-model="formData.departmentID"
            :options="deptOptions"
            :show-count="true"
            placeholder="请选择部门"
          />
        </el-form-item>

        <el-form-item label="工序" prop="procId" required>
          <el-input
            v-model="processName"
            placeholder="请选择"
            clearable
            :style="{ width: '100%' }"
            readonly
            @click.native="visiblea = true"
          ></el-input>
        </el-form-item>

        <el-form-item label="状态" prop="status" required>
          <el-switch
            v-model="formData.status"
            :active-value=0
            :inactive-value=1
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="formData.remark"
            type="textarea"
            placeholder="请输入备注"
            :autosize="{ minRows: 4, maxRows: 4 }"
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="onClose">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
    </el-dialog>
    <div>
      <!--   工序选择     -->
      <processList ref="workCenterList" :visible.sync="visiblea" @Ok="processOk"/>
    </div>
  </div>
</template>
<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import processList from "../basic_processAction/processList";
import { deptTreeSelect } from "@/api/system/user";
export default {
  inheritAttrs: false,
  components: { Treeselect,processList },
  props: ["title", "visible", "item"],
  data() {
    return {
      formData: {
        code: "",
        name: "",
        departmentID:undefined,
        procId:"",
        status: 0,
      },
      rules: {
        code: [
          {
            required: true,
            message: "请输入工作中心编码",
            trigger: "blur",
          },
        ],
        name: [
          {
            required: true,
            message: "请输入工作中心名称",
            trigger: "blur",
          },
        ],
        departmentID: [
          {
            required: true,
            message: "请选择部门",
            trigger: "blur",
          },
        ],
        procId: [
          {
            required: true,
            message: "请选择工序",
            trigger: "blur",
          },
        ],
      },
      deptOptions: [],

      /**
       * 工序选择
       */
      visiblea:false,
      processName:""
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async getDictText() {
      this.$nextTick(() => {
        if (this.item) {
          this.formData = JSON.parse(JSON.stringify(this.item));
          this.processName = this.item.procIdText;
        }
      });
    },
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    onOpen() {
      this.getDictText();
      this.getDeptTree();
    },
    onClose() {
      this.$refs["elForm"].resetFields();
      this.processName = "";
      this.close();
    },
    close() {
      this.$emit("update:visible", false);
    },
    handleConfirm() {
      let form = JSON.parse(JSON.stringify(this.formData));
      console.log(form);
      this.$refs["elForm"].validate((valid) => {
        if (!valid) return;
        this.$emit("Ok", form);
        this.close();
      });
    },

    // 子页面数据接收
    processOk(item) {
      this.formData.procId = item.id;
      this.processName = item.name;
      this.visiblea = false;
    }
  },
};
</script>
<style>
</style>
